import Util from "../../utils/util"
import api from '../../utils/api'
import moment from '../../utils/moment'
const app = getApp();
var appGlobal = getApp();
// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    intrc_url: '',//规则说明跳转图片地址
    intrc_title: '',//规则说明标题
    adv_page: 1,
    intrc_isShow: 1,//规则广告是否显示默认为显示
    footer_adv_list: [],
    awardAmount:0,//奖励金币
    regulation_btn:true,
    isVersionShow: false
  },
  goRule: function (e) {

    var _this = this;
    var intrc_url = _this.data.intrc_url;

    if (intrc_url != '') {
      //规则说明
      if (_this.data.regulation_btn=="true"){
        wx.navigateTo({
          url: '/pages/rule/rule?url=' + intrc_url
        })
      }
    }
  },
  //获取广告
  getAdv: function () {
    var _this = this;
    var token = wx.getStorageSync('token');
    var url = 'index.php?g=Api&m=GobalConf&a=getAdv';
    api.post(url, { type: app.setConfig.type, token: token }, function (res) {
      var data = res.data;
      _this.setData({
        intrc_title: data.intro_adv.ad_name,
        intrc_url: data.intro_adv.adv_info_url,
      });
      if (data.idex_footer != "") {
        _this.setData({
          footer_adv_list: data.index_footer
        });
      }
    },false)
  },
  goRanking: function () {
    wx.navigateTo({
      url: '/pages/ranking/ranking',
    })
  },
  goTitleLlist: function () {
    wx.navigateTo({
      url: '/pages/titleLlist/titleLlist',
    });
  },
  goStart: function () {
    wx.navigateTo({
      url: '/pages/guessSong/guessSong',
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    this.loop(options);
  },
  loop:function(options){
    wx.showLoading({
      title: '加载中...',
      mask:'true'
    })
    var _this = this;
    var token = wx.getStorageSync('token');
    if(token){
      _this.defaultConfig();
      _this.versionControll();
      //加载广告
      _this.getAdv();
      //处理好友关系
      _this.createFriends(options);
      //登录奖励
      _this.loginAward();
  
    }else{
      setTimeout(function () { _this.loop(options); }, 100);
    }
  },
  //获取登录信息
  onShow: function () {
    var _this = this;
    app.wxPost(app.setConfig.url + '/index.php/Api/version', { version: 'v1' }, function (e) {
      if (e.data.data.v1 != false) {
        _this.setData({
          isVersionShow: true,
          firstShowPage: e.data.data.firstShowPage,
        });
        wx.setNavigationBarTitle({
          title: '猜成语',
        })
      } else {
        _this.setData({
          firstShowPage: e.data.data.firstShowPage
        });
        if (e.data.data.firstShowPage == 'flash') {
          wx.setNavigationBarTitle({
            title: '艾北科技',
          })
        } else {
          wx.setNavigationBarTitle({
            title: '加载中',
          })
        }
      }
    });
  },
  versionControll(){
    var _this = this;
    app.wxPost(app.setConfig.url + '/index.php/Api/version', { version: 'v1' }, function (e) {
      if (e.data.data.v1 != false) {
        _this.setData({
          isVersionShow: true,
          firstShowPage: e.data.data.firstShowPage
        });
        wx.setNavigationBarTitle({
          title: '猜成语',
        })
      } else {
        _this.setData({
          firstShowPage: e.data.data.firstShowPage
        });
        if (e.data.data.firstShowPage == 'flash') {
          wx.setNavigationBarTitle({
            title: '艾北科技',
          })
        } else {
          wx.setNavigationBarTitle({
            title: '加载中',
          })
        }
      }
    });
  },
  defaultConfig:function(){
    var _this = this;
    var token = wx.getStorageSync('token');

    api.post('index.php?g=Api&m=GobalConf&a=getRunDataConfig', { token: token, type: app.setConfig.type }, function (res) {
     
      if (res.code == 2000) {
        //that.globalData.defaultConfigs = res.data;
        //console.log('我来看看这个时候的数据是什么');
        ///console.log(that.globalData.defaultConfigs);
       // console.log("加载数据");
        //console.log(res);
         _this.setData({
           regulation_btn: res.data.regulation_btn
         })
      }
    }, false);
  },
  createFriends:function(options){
    //建立并处理好友关系
        var token = wx.getStorageSync('token');
        var friendid = options.friendid;
        if(friendid!='' || friendid!=undefined){
          var uri = 'index.php?g=Api&m=Share&a=createFriends'
          api.post(uri, { token: token, friendid: friendid }, function (e) {
            console.log(e);
          }, false);
        }
    
  },
  onReady:function(e){
    this.awarddialog = this.selectComponent("#awarddialog");
  },
  //记载更多广告
  loadMoreAdvEvent: function (res) {
    console.log("试试是看");
    var _this = this;
    var page = _this.data.adv_page;
    page = page + 1;
    _this.setData({
      adv_page: page
    })
    var token = wx.getStorageSync('token');
    var url = 'index.php?g=Api&m=GobalConf&a=getAdv';
    api.post(url, { type: app.setConfig.type, token: token, page: page, adv_location: 1 }, function (res) {
      var data = res.data;
      if (data.index_footer != '') {
        var footer_adv_list = _this.data.footer_adv_list;


        for (var i = 0; i < data.index_footer.length; i++) {
          footer_adv_list.push(data.index_footer[i]);

        }
        //console.log(footer_adv_list);
        _this.setData({
          footer_adv_list: footer_adv_list
        });
      }
    },false);
  },
  advEnvet: function (e) {
    console.log("这是我的测试")
    console.log(e.target.dataset);
    if (e.target.dataset.advtype == 1) {
      //APP 跳转广告
      var target = e.target;
      wx.navigateToMiniProgram({
        appId: target.appid.toString(),
        path: target.page.toString(),
        extraData: target.sence,
      });
    } else {
      console.log(e);
      //普通跳转广告
      wx.navigateTo({
        url: '/pages/rule/rule?url=' + e.currentTarget.dataset.infourl
      })

    }
  },
  loginAward:function(){
    var _this = this;
    var token = wx.getStorageSync('token');
    var url = 'index.php?g=User&m=Consumer&a=loginAward';
    api.post(url, { type: app.setConfig.type, token: token}, function (res) {
        console.log(res);
        if(res.code==2000){
          if (res.data.award_amount > 0){
             _this.setData({
               awardAmount: res.data.award_amount
             });
             _this.awarddialog.showDialog();
          }
        }
    },false)
  }
})